package com.tuanzi.loan.business.service.collaborate.impl;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.entity.LoginUser;
import com.tuanzi.loan.business.entity.statistics.AdverChannelTypeUser;
import com.tuanzi.loan.business.repository.collaborate.CollaBorateAdvertiseTransFormationRepository;
import com.tuanzi.loan.business.repository.statistics.AdverChannelTypeUserRepository;
import com.tuanzi.loan.business.service.collaborate.CollaBorateAdvertiseTransFormationService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.advertise.AdvertiseTransStatRequest;
import com.tuanzi.loan.vo.response.advertise.AdvertiseTransStatResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author Simon
 * @date 2019/07/29 14:29
 */
@Slf4j
@Debug
@Service
@Transactional
@Retryable(value = {RetryException.class})
public class CollaBorateAdvertiseTransFormationServiceImpl implements CollaBorateAdvertiseTransFormationService {

    @Autowired
    private CollaBorateAdvertiseTransFormationRepository loanAdvertiseTransFormationRepository;
    @Autowired
    private AdverChannelTypeUserRepository adverChannelTypeUserRepository;


    @Override
    public Page<AdvertiseTransStatResponse> list(PageSearcher<AdvertiseTransStatRequest> pageSearcher) {
        if (Objects.isNull(pageSearcher))
            throw new NullPointerException("pageSearcher is null");
        if (Objects.isNull(pageSearcher.getSearchCondition()) || Objects.isNull(pageSearcher.getPageable()))
            throw new NullPointerException("activityparticipatestatSearchRequest or pageable is null");
        LoginUser loginUser = CurrentUser.get();
        if(null == loginUser){
            return new PageImpl<>(Lists.newArrayList());
        }
        String account = loginUser.getAccount();
        if(StringUtils.isBlank(account)){
            return new PageImpl<>(Lists.newArrayList());
        }
        List<AdverChannelTypeUser> ctUser = adverChannelTypeUserRepository.findCurrentUserChannels(account);
        if (Objects.isNull(ctUser) || ctUser.size() <= 0){
            return new PageImpl<>(Lists.newArrayList());
        }
        pageSearcher.getSearchCondition().setAdsPlatformType(ctUser.get(0).getAdsPlatformType());

        return loanAdvertiseTransFormationRepository.list(pageSearcher.getSearchCondition(),pageSearcher.getPageable(),ctUser);
    }

    @Override
    public String getExcel(AdvertiseTransStatRequest request) {
        List<AdvertiseTransStatResponse> list = getDataList(request);
        StringBuilder sf = new StringBuilder();
        sf.append("时间,计划标识,广告平台,手机平台,渠道号,回传数\r\n");
        for (AdvertiseTransStatResponse advertiseTransStatResponse: list) {
            sf.append(advertiseTransStatResponse.getTime()).append(",");
            sf.append(advertiseTransStatResponse.getAdid()).append(",");
            sf.append(advertiseTransStatResponse.getAdsPlatformType()).append(",");
            sf.append(advertiseTransStatResponse.getPlatform()).append(",");
            sf.append(advertiseTransStatResponse.getChannel()).append(",");
            sf.append(advertiseTransStatResponse.getCount()).append(",");
            sf.append("\r\n");
        }
        return sf.toString();
    }
    @Override
    public Long getCountSum() {
        return loanAdvertiseTransFormationRepository.getCountSum();
    }

    public List<AdvertiseTransStatResponse> getDataList(AdvertiseTransStatRequest searchRequest) {
        return loanAdvertiseTransFormationRepository.getDataList(searchRequest);
    }
}
